package com.link.base.tencent.corpwx.addressbook.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.link.base.tencent.corpwx.addressbook.model.Department;
import com.link.base.wechat.basic.util.WxExceptionUtil;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.util.HttpRequestUtil;
import com.link.core.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 企业微信部门管理接口
 *
 * @author 欧阳振强
 * @date 2019/1/3
 */
public class DeptUtil {
    /**获取部门列表接口*/
    private static final String LIST_DEPARTMENT_API = "https://qyapi.weixin.qq.com/cgi-bin/department/list";
    /**新增部门接口*/
    private static final String NEW_DEPARTMENT_API = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=ACCESS_TOKEN";
    /**更新部门接口*/
    private static final String UPDATE_DEPARTMENT_API = "https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token=ACCESS_TOKEN";
    /**删除部门接口*/
    private static final String DEL_DEPARTMENT_API = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token=ACCESS_TOKEN&id=ID";

    /**
     * 查询部门-返回string
     *
     * @author 欧阳振强
     * @date 2019/1/3
     * @param accessToken
     * @param deptId
     */
    public static JSONObject queryDepartmentListResultString(String accessToken, String deptId) throws Exception {
        if (StringUtils.isBlank(accessToken)) {
            throw new BasicServiceException("accessToken不能为空！");
        }
        String url = LIST_DEPARTMENT_API;
        String param = "access_token=" + accessToken;
        if (StringUtils.isNotBlank(deptId)) {
            param = param + "&id=" + deptId;
        }
        String resultStr = HttpRequestUtil.sendGet(url, param);
        return WxExceptionUtil.parse(resultStr);
    }

    /**
     * 查询部门-返回list
     *
     * @author 欧阳振强
     * @date 2019/1/3
     */
    public static List<Department> queryDepartmentListResultList(String accessToken, String deptId) throws Exception {
        JSONObject jsonObject = queryDepartmentListResultString(accessToken, deptId);
        JSONArray departmentList = jsonObject.getJSONArray("department");
        List<Department> list = new ArrayList<>();
        for (int i = 0; i < departmentList.size(); i++) {
            Department department = new Department();
            JSONObject obj = departmentList.getJSONObject(i);
            department.setDeptId(obj.getString("id"));
            department.setDeptName(obj.getString("name"));
            department.setParentId(obj.getString("parentid"));
            department.setOrder(obj.getString("order"));
            list.add(department);
        }
        return list;
    }

    /**
     * 新增部门
     *
     * @author 欧阳振强
     * @date 2019/1/3
     * @param accessToken
     * @param dept 部门对象
     */
    public static JSONObject newWxDept(String accessToken, Department dept) throws Exception {
        if (StringUtils.isBlank(accessToken)) {
            throw new BasicServiceException("accessToken不能为空！");
        }
        String url = NEW_DEPARTMENT_API.replace("ACCESS_TOKEN", accessToken);
        JSONObject obj = new JSONObject();
        obj.put("name", dept.getDeptName());
        obj.put("parentid", dept.getParentId());
        String param = obj.toJSONString();
        String resultStr = HttpRequestUtil.sendPost(url, param, "");
        return WxExceptionUtil.parse(resultStr);
    }

    /**
     * 更新部门
     *
     * @author 欧阳振强
     * @date 2019/1/3
     * @param accessToken
     * @param dept 部门对象
     */
    public static JSONObject updateWxDept(String accessToken, Department dept) throws Exception {
        if (StringUtils.isBlank(accessToken)) {
            throw new BasicServiceException("accessToken不能为空！");
        }
        String url = UPDATE_DEPARTMENT_API.replace("ACCESS_TOKEN", accessToken);
        JSONObject obj = new JSONObject();
        obj.put("id",  dept.getDeptId());
        obj.put("name", dept.getDeptName());
        obj.put("parentid", dept.getParentId());
        String param = obj.toJSONString();
        String resultStr = HttpRequestUtil.jsonPost(url, param);
        return WxExceptionUtil.parse(resultStr);
    }

    /**
     * 删除部门
     *
     * @author 欧阳振强
     * @date 2019/1/3
     * @param accessToken
     * @param deptId
     */
    public static JSONObject deleteWxDept(String accessToken, String deptId) throws Exception {
        if (StringUtils.isBlank(accessToken)) {
            throw new BasicServiceException("accessToken不能为空！");
        }
        if (StringUtils.isBlank(deptId)) {
            throw new BasicServiceException("deptId不能为空！");
        }
        String url = DEL_DEPARTMENT_API.replace("ACCESS_TOKEN", accessToken).replace("ID", deptId);
        String resultStr = HttpRequestUtil.sendGet(url, "");
        return WxExceptionUtil.parse(resultStr);
    }

    /**
     * 删除部门下的子部门
     *
     * @author 欧阳振强
     * @date 2019/1/3
     * @param accessToken
     * @param deptId
     */
    public static void deleteChildWxDept(String accessToken, String deptId) throws Exception {
        if (StringUtils.isBlank(accessToken)) {
            throw new BasicServiceException("accessToken不能为空！");
        }
        if (StringUtils.isBlank(deptId)) {
            throw new BasicServiceException("deptId不能为空！");
        }
        List<Department> list = queryDepartmentListResultList(accessToken, deptId);
        for (Department item : list) {
            String childDeptId = item.getDeptId();
            if (!deptId.equals(childDeptId)) {
                deleteWxDept(accessToken, childDeptId);
            }
        }
    }
}
